package org.elanfox.intellicommunity.intellicommunityadmin.controller.msg;

import org.elanfox.intellicommunity.biz.dto.condition.MsgSearchCondition;
import org.elanfox.intellicommunity.biz.entity.msg.AlarmMsgPO;
import org.elanfox.intellicommunity.biz.entity.msg.LocationMsgPO;
import org.elanfox.intellicommunity.biz.service.core.msg.AlarmMsgService;
import org.elanfox.intellicommunity.biz.service.core.msg.LocationMsgService;
import org.elanfox.intellicommunity.biz.service.core.msg.NodeMsgService;
import org.elanfox.intellicommunity.common.base.ApiResult;
import org.elanfox.intellicommunity.common.base.ResultCodeEnum;
import org.elanfox.intellicommunity.common.constant.Constant;
import org.elanfox.intellicommunity.msg.constant.MsgConstant;
import org.elanfox.intellicommunity.msg.dto.RawLocationMsgDTO;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author allan
 * @version 1.0.0
 * @date 2019/12/20
 */
@Controller
@RequestMapping("/msg")
public class MsgController {
    @Autowired
    private LocationMsgService locationMsgService;
    @Autowired
    private AlarmMsgService alarmMsgService;
    @Autowired
    private NodeMsgService nodeMsgService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/location")
    public String location(
            @ModelAttribute("condition") MsgSearchCondition condition,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        Page<LocationMsgPO> page = locationMsgService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);

        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", page.getTotalElements());
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("list", page.getContent());

        return "msg/location_msg_list";
    }

    @GetMapping("/alarm")
    public String alarm(
            @ModelAttribute("condition") MsgSearchCondition condition,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        Page<AlarmMsgPO> page = alarmMsgService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);

        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", page.getTotalElements());
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("list", page.getContent());

        return "msg/alarm_msg_list";
    }

    /**
     * 消息模拟
     *
     * @return
     */
    @PostMapping("/simulation")
    @ResponseBody
    public ApiResult simulation(RawLocationMsgDTO rawLocationMsgDTO, int msgType) {
        Map<String, Object> msg = new HashMap<>();
        msg.put("msg", rawLocationMsgDTO);
        rabbitTemplate.convertAndSend(MsgConstant.EXCHANGE_MESSAGE, MsgConstant.QUEUE_LOCATION, msg);
        if (msgType == 1) {
            rabbitTemplate.convertAndSend(MsgConstant.EXCHANGE_MESSAGE, MsgConstant.QUEUE_ALARM, msg);
        }

        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }
}
